package com.cyx.exam.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cyx.exam.entity.User;
import com.cyx.exam.mapper.UserMapper;
import com.cyx.exam.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author cyx
 * @since 2021-03-09
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Resource
    private UserMapper userMapper;

    @Override
    public IPage<User> selectByPage(int start, int size) {
        Page<User> page=new Page<>(start,size);
        userMapper.selectPage(page, null);
        return page;
    }

    @Override
    public IPage<User> searchByPage(int start,int size, String name) {
        Page<User> page=new Page<>(start,size);
        userMapper.selectPage(page, new QueryWrapper<User>().like("name", "%"+name+"%").
                            or().like("class_name","%"+name+"%"));
        return page;
    }


    @Override
    public int delete(Integer id) {
        QueryWrapper<User> wrapper = new QueryWrapper<User>();
        wrapper.eq("id",id);
        return userMapper.delete(wrapper);
    }

    @Override
    public List<String> getNameList() {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.select("name");
        List<User> classes = userMapper.selectList(wrapper);
        return classes.stream().map(User::getName).collect(Collectors.toList());// 取出其中一列
    }

    @Override
    public void updateByName(User user) {
        userMapper.update(user,new UpdateWrapper<User>().eq("name",user.getName()));
    }
}
